package com.rybbaby.tss.platform.sysTableColumn.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import com.rybbaby.tss.core.annotation.RequestLog;
import com.rybbaby.tss.core.annotation.Token;
import com.rybbaby.tss.core.bean.TableColumnBean;
import com.rybbaby.tss.core.utils.AjaxJsonResponse;
import com.rybbaby.tss.core.utils.SequenceUtil;
import com.rybbaby.tss.core.utils.StringUtils;
import com.rybbaby.tss.core.web.controller.BaseController;
import com.rybbaby.tss.platform.codeGenerate.service.ICodeGenerateService;
import com.rybbaby.tss.platform.sysTableColumn.entity.SysTableColumnMapping;
import com.rybbaby.tss.platform.sysTableColumn.service.ISysTableColumnMappingService;
import org.apache.commons.lang.BooleanUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * @description 数据库表映射管理
 * 
 * @author 谢进伟
 * 
 * @createDate 2016年2月25日 上午9:21:56
 */
@Controller
@RequestMapping(value = "sysTableColumnMappingController")
public class SysTableColumnMappingController extends BaseController {
	
	@Autowired
	private ISysTableColumnMappingService sysTableColumnMappingService;
	@Autowired
	private ICodeGenerateService codeGenerateService;
	
	@ResponseBody
	@RequestLog(content = "修改列的映射相关值")
	@Token(validateToken = false)
	@RequestMapping(value = "updateColumnMapping" , method = RequestMethod.POST)
	public String updateColumnMapping() {
		AjaxJsonResponse ajr = new AjaxJsonResponse();
		try {
			String tableName = getParameter("tableName");
			String id = getParameter("id");
			String columnJavaFieldName = getParameter("columnJavaFieldName");
			String columnJavaType = getParameter("columnJavaType");
			String isSearchable = getParameter("isSearchable");
			String isNullable = getParameter("isNullable");
			String isAddCanEdit = getParameter("isAddCanEdit");
			String isUpdateCanEdit = getParameter("isUpdateCanEdit");
			String isCreateTime = getParameter("isCreateTime");
			String isModifiedTime = getParameter("isCreateTime");
			String datasourceName = getParameter("datasourceName");
			String datasourceCatalog = getParameter("datasourceCatalog");
			String datasourceTableName = getParameter("datasourceTableName");
			String datasourceKeyColumn = getParameter("datasourceKeyColumn");
			String datasourceValueColumn = getParameter("datasourceValueColumn");
			String datasourceSql = getParameter("datasourceSql");
			String referenceCatalog = getParameter("referenceCatalog");
			String referenceTableName = getParameter("referenceTableName");
			String uploadFileConfig = getParameter("uploadFileConfig");
			if(StringUtils.isBlank(columnJavaFieldName) || StringUtils.isBlank(columnJavaType) || StringUtils.isBlank(isSearchable) || StringUtils.isBlank(isNullable) || StringUtils.isBlank(isAddCanEdit) || StringUtils.isBlank(isUpdateCanEdit) || StringUtils.isBlank(isCreateTime) || StringUtils.isBlank(isModifiedTime)) {
				ajr.setResMsg("参数不正确!");
				ajr.setResCode(300);
			} else {
				SysTableColumnMapping sysTableColumnMapping = this.sysTableColumnMappingService.get(id);
				if(sysTableColumnMapping != null) {
					DetachedCriteria dc = DetachedCriteria.forClass(SysTableColumnMapping.class);
					dc.add(Restrictions.eq("columnJavaFieldName" , columnJavaFieldName));
					dc.add(Restrictions.eq("tableName" , tableName));
					List<SysTableColumnMapping> list = this.sysTableColumnMappingService.findByDetached(dc);
					if(list != null && !list.isEmpty()) {
						if(!list.get(0).getId().equals(sysTableColumnMapping.getId())) {
							ajr.setResMsg("此映射已存在,请重新设置属性名!");
							ajr.setResCode(-1);
							return ajr.jsonResponse(false);
						}
					}
					if(StringUtils.isNotBlank(referenceCatalog) && StringUtils.isNotBlank(referenceTableName)) {
						List<TableColumnBean> columns = this.codeGenerateService.getTableColumns(referenceCatalog , referenceTableName);
						List<String> referenceTableShowColumns = new ArrayList<String>();
						List<String> referenceTableSearchColumns = new ArrayList<String>();
						for(TableColumnBean tableColumnBean : columns) {
							String columnName = tableColumnBean.getColumnName();
							Boolean isShow = BooleanUtils.toBoolean(getParameter(referenceTableName + "_show_" + columnName));
							Boolean isSearch = BooleanUtils.toBoolean(getParameter(referenceTableName + "_search_" + columnName));
							if(isShow) {
								referenceTableShowColumns.add(columnName);
							}
							if(isSearch) {
								referenceTableSearchColumns.add(columnName);
							}
						}
						if(!referenceTableShowColumns.isEmpty()) {
							sysTableColumnMapping.setReferenceTableShowColumns(StringUtils.join(referenceTableShowColumns , ","));
						} else {
							sysTableColumnMapping.setReferenceTableShowColumns(null);
						}
						if(!referenceTableSearchColumns.isEmpty()) {
							sysTableColumnMapping.setReferenceTableSearchColumns(StringUtils.join(referenceTableSearchColumns , ","));
						} else {
							sysTableColumnMapping.setReferenceTableSearchColumns(null);
						}
					}
					sysTableColumnMapping.setColumnJavaFieldName(columnJavaFieldName);
					sysTableColumnMapping.setColumnJavaType(columnJavaType);
					sysTableColumnMapping.setIsSearchable(BooleanUtils.toBooleanObject(isSearchable));
					sysTableColumnMapping.setIsNullable(BooleanUtils.toBooleanObject(isNullable));
					sysTableColumnMapping.setIsAddCanEdit(BooleanUtils.toBooleanObject(isAddCanEdit));
					sysTableColumnMapping.setIsUpdateCanEdit(BooleanUtils.toBooleanObject(isUpdateCanEdit));
					sysTableColumnMapping.setIsCreateTime(BooleanUtils.toBooleanObject(isCreateTime));
					sysTableColumnMapping.setIsModifiedTime(BooleanUtils.toBooleanObject(isModifiedTime));
					sysTableColumnMapping.setDatasourceName(datasourceName);
					sysTableColumnMapping.setDatasourceCatalog(datasourceCatalog);
					sysTableColumnMapping.setDatasourceTableName(datasourceTableName);
					sysTableColumnMapping.setDatasourceKeyColumn(datasourceKeyColumn);
					sysTableColumnMapping.setDatasourceValueColumn(datasourceValueColumn);
					sysTableColumnMapping.setDatasourceSql(datasourceSql);
					sysTableColumnMapping.setUploadConfig(uploadFileConfig);
					this.sysTableColumnMappingService.updateEntitiy(sysTableColumnMapping);
					ajr.setResMsg("修改成功!");
				}
			}
		} catch (Exception e) {
			ajr.setResMsg("服务器端异常(" + e.getMessage() + ")!");
			ajr.setResCode(500);
			e.printStackTrace();
		}
		return ajr.jsonResponse(false);
	}
	
	@ResponseBody
	@RequestLog(content = "获取指定映射表的映射信息")
	@RequestMapping(value = "updateColumnMapping" , method = RequestMethod.GET)
	public String updateColumnMapping(HttpServletResponse response) throws IOException {
		AjaxJsonResponse ajr = new AjaxJsonResponse("获取成功!");
		try {
			String tableName = getParameter("tableName");
			String columnId = getParameter("columnId");
			SysTableColumnMapping columnMapping = this.sysTableColumnMappingService.get(columnId);
			if(columnMapping != null) {
				columnMapping.setReferenceTable(tableName);
				this.sysTableColumnMappingService.updateEntitiy(columnMapping);
			} else {
				throw new Exception("未找到指定映射列!");
			}
		} catch (Exception e) {
			ajr.setResCode(500);
			ajr.setResMsg(e.getMessage());
			e.printStackTrace();
		}
		return ajr.jsonResponse(false);
	}
	
	@ResponseBody
	@RequestLog(content = "获取指定映射表的映射信息")
	@RequestMapping(value = "getTableMappingJsonStr" , method = RequestMethod.GET)
	public String getTableMappingJsonStr(HttpServletResponse response) throws IOException {
		AjaxJsonResponse ajr = new AjaxJsonResponse("获取成功!");
		try {
			String catalog = getParameter("catalog");
			String tableName = getParameter("tableName");
			DetachedCriteria dc = DetachedCriteria.forClass(SysTableColumnMapping.class);
			dc.add(Restrictions.eq("scheme" , catalog));
			dc.add(Restrictions.eq("tableName" , tableName));
			List<SysTableColumnMapping> list = this.sysTableColumnMappingService.findByDetached(dc);
			ajr.setData(list);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ajr.jsonResponse(true);
	}
	
	@ResponseBody
	@RequestLog(content = "获取指定映射列的映射信息")
	@RequestMapping(value = "getColumnsMappingJsonStr" , method = RequestMethod.GET)
	public String getColumnsMappingJsonStr(HttpServletResponse response) throws IOException {
		AjaxJsonResponse ajr = new AjaxJsonResponse("获取成功!");
		try {
			String catalog = getParameter("catalog");
			String tableName = getParameter("tableName");
			String columnName = getParameter("columnName");
			DetachedCriteria dc = DetachedCriteria.forClass(SysTableColumnMapping.class);
			dc.add(Restrictions.eq("scheme" , catalog));
			dc.add(Restrictions.eq("tableName" , tableName));
			dc.add(Restrictions.eq("columnName" , columnName));
			List<SysTableColumnMapping> list = this.sysTableColumnMappingService.findByDetached(dc);
			if(list != null && !list.isEmpty()) {
				ajr.setData(list.get(0));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ajr.jsonResponse(true);
	}
	
	public static void main(String [] args)  {
		List<String> list = new ArrayList<String>();
		list.add("INSERT INTO sys_column_event_dictionary (id, eventName, triggerFunction, isOnlySupportHtml5, remark,weight) VALUES (\"%s\",\"onafterprint\", \"function(){}\", 0,\"文档打印之后运行的脚本。\", 0)");
		list.add("INSERT INTO sys_column_event_dictionary (id, eventName, triggerFunction, isOnlySupportHtml5, remark,weight) VALUES (\"%s\",\"onbeforeprint\", \"function(){}\", 0,\"文档打印之前运行的脚本。\", 0)");
		list.add("INSERT INTO sys_column_event_dictionary (id, eventName, triggerFunction, isOnlySupportHtml5, remark,weight) VALUES (\"%s\",\"onbeforeunload\", \"function(){}\", 0,\"文档卸载之前运行的脚本。\", 0)");
		list.add("INSERT INTO sys_column_event_dictionary (id, eventName, triggerFunction, isOnlySupportHtml5, remark,weight) VALUES (\"%s\",\"onerror\", \"function(){}\", 0,\"在错误发生时运行的脚本。\", 0)");
		list.add("INSERT INTO sys_column_event_dictionary (id, eventName, triggerFunction, isOnlySupportHtml5, remark,weight) VALUES (\"%s\",\"onhaschange\", \"function(){}\", 0,\"当文档已改变时运行的脚本。\", 0)");
		list.add("INSERT INTO sys_column_event_dictionary (id, eventName, triggerFunction, isOnlySupportHtml5, remark,weight) VALUES (\"%s\",\"onload\", \"function(){}\", 1,\"页面结束加载之后触发。\", 0)");
		list.add("INSERT INTO sys_column_event_dictionary (id, eventName, triggerFunction, isOnlySupportHtml5, remark,weight) VALUES (\"%s\",\"onmessage\", \"function(){}\", 0,\"在消息被触发时运行的脚本。\", 0)");
		list.add("INSERT INTO sys_column_event_dictionary (id, eventName, triggerFunction, isOnlySupportHtml5, remark,weight) VALUES (\"%s\",\"onoffline\", \"function(){}\", 0,\"当文档离线时运行的脚本。\", 0)");
		list.add("INSERT INTO sys_column_event_dictionary (id, eventName, triggerFunction, isOnlySupportHtml5, remark,weight) VALUES (\"%s\",\"ononline\", \"function(){}\", 0,\"当文档上线时运行的脚本。\", 0)");
		list.add("INSERT INTO sys_column_event_dictionary (id, eventName, triggerFunction, isOnlySupportHtml5, remark,weight) VALUES (\"%s\",\"onpagehide\", \"function(){}\", 0,\"当窗口隐藏时运行的脚本。\", 0)");
		list.add("INSERT INTO sys_column_event_dictionary (id, eventName, triggerFunction, isOnlySupportHtml5, remark,weight) VALUES (\"%s\",\"onpageshow\", \"function(){}\", 0,\"当窗口成为可见时运行的脚本。\", 0)");
		list.add("INSERT INTO sys_column_event_dictionary (id, eventName, triggerFunction, isOnlySupportHtml5, remark,weight) VALUES (\"%s\",\"onpopstate\", \"function(){}\", 0,\"当窗口历史记录改变时运行的脚本。\", 0)");
		list.add("INSERT INTO sys_column_event_dictionary (id, eventName, triggerFunction, isOnlySupportHtml5, remark,weight) VALUES (\"%s\",\"onredo\", \"function(){}\", 0,\"当文档执行撤销（redo）时运行的脚本。\", 0)");
		list.add("INSERT INTO sys_column_event_dictionary (id, eventName, triggerFunction, isOnlySupportHtml5, remark,weight) VALUES (\"%s\",\"onresize\", \"function(){}\", 0,\"当浏览器窗口被调整大小时触发。\", 0)");
		list.add("INSERT INTO sys_column_event_dictionary (id, eventName, triggerFunction, isOnlySupportHtml5, remark,weight) VALUES (\"%s\",\"onstorage\", \"function(){}\", 0,\"在 Web Storage 区域更新后运行的脚本。\", 0)");
		list.add("INSERT INTO sys_column_event_dictionary (id, eventName, triggerFunction, isOnlySupportHtml5, remark,weight) VALUES (\"%s\",\"onundo\", \"function(){}\", 0,\"在文档执行 undo 时运行的脚本。\", 0)");
		list.add("INSERT INTO sys_column_event_dictionary (id, eventName, triggerFunction, isOnlySupportHtml5, remark,weight) VALUES (\"%s\",\"onunload\", \"function(){}\", 1,\"一旦页面已下载时触发（或者浏览器窗口已被关闭）。\", 0)");
		list.add("INSERT INTO sys_column_event_dictionary (id, eventName, triggerFunction, isOnlySupportHtml5, remark,weight) VALUES (\"%s\",\"onblur\", \"function(){}\", 1,\"元素失去焦点时运行的脚本。\", 0)");
		list.add("INSERT INTO sys_column_event_dictionary (id, eventName, triggerFunction, isOnlySupportHtml5, remark,weight) VALUES (\"%s\",\"onchange\", \"function(){}\", 1,\"在元素值被改变时运行的脚本。\", 0)");
		list.add("INSERT INTO sys_column_event_dictionary (id, eventName, triggerFunction, isOnlySupportHtml5, remark,weight) VALUES (\"%s\",\"oncontextmenu\", \"function(){}\", 0,\"当上下文菜单被触发时运行的脚本。\", 0)");
		list.add("INSERT INTO sys_column_event_dictionary (id, eventName, triggerFunction, isOnlySupportHtml5, remark,weight) VALUES (\"%s\",\"onfocus\", \"function(){}\", 1,\"当元素失去焦点时运行的脚本。\", 0)");
		list.add("INSERT INTO sys_column_event_dictionary (id, eventName, triggerFunction, isOnlySupportHtml5, remark,weight) VALUES (\"%s\",\"onformchange\", \"function(){}\", 0,\"在表单改变时运行的脚本。\", 0)");
		list.add("INSERT INTO sys_column_event_dictionary (id, eventName, triggerFunction, isOnlySupportHtml5, remark,weight) VALUES (\"%s\",\"onforminput\", \"function(){}\", 0,\"当表单获得用户输入时运行的脚本。\", 0)");
		list.add("INSERT INTO sys_column_event_dictionary (id, eventName, triggerFunction, isOnlySupportHtml5, remark,weight) VALUES (\"%s\",\"oninput\", \"function(){}\", 0,\"当元素获得用户输入时运行的脚本。\", 0)");
		list.add("INSERT INTO sys_column_event_dictionary (id, eventName, triggerFunction, isOnlySupportHtml5, remark,weight) VALUES (\"%s\",\"oninvalid\", \"function(){}\", 0,\"当元素无效时运行的脚本。\", 0)");
		list.add("INSERT INTO sys_column_event_dictionary (id, eventName, triggerFunction, isOnlySupportHtml5, remark,weight) VALUES (\"%s\",\"onreset\", \"function(){}\", 1,\"当表单中的重置按钮被点击时触发。HTML5 中不支持。\", 0)");
		list.add("INSERT INTO sys_column_event_dictionary (id, eventName, triggerFunction, isOnlySupportHtml5, remark,weight) VALUES (\"%s\",\"onselect\", \"function(){}\", 1,\"在元素中文本被选中后触发。\", 0)");
		list.add("INSERT INTO sys_column_event_dictionary (id, eventName, triggerFunction, isOnlySupportHtml5, remark,weight) VALUES (\"%s\",\"onsubmit\", \"function(){}\", 1,\"在提交表单时触发。\", 0)");
		list.add("INSERT INTO sys_column_event_dictionary (id, eventName, triggerFunction, isOnlySupportHtml5, remark,weight) VALUES (\"%s\",\"onkeydown\", \"function(){}\", 1,\"在用户按下按键时触发。\", 0)");
		list.add("INSERT INTO sys_column_event_dictionary (id, eventName, triggerFunction, isOnlySupportHtml5, remark,weight) VALUES (\"%s\",\"onkeypress\", \"function(){}\", 1,\"在用户敲击按钮时触发。\", 0)");
		list.add("INSERT INTO sys_column_event_dictionary (id, eventName, triggerFunction, isOnlySupportHtml5, remark,weight) VALUES (\"%s\",\"onkeyup\", \"function(){}\", 1,\"当用户释放按键时触发。\", 0)");
		list.add("INSERT INTO sys_column_event_dictionary (id, eventName, triggerFunction, isOnlySupportHtml5, remark,weight) VALUES (\"%s\",\"onclick\", \"function(){}\", 1,\"元素上发生鼠标点击时触发。\", 0)");
		list.add("INSERT INTO sys_column_event_dictionary (id, eventName, triggerFunction, isOnlySupportHtml5, remark,weight) VALUES (\"%s\",\"ondblclick\", \"function(){}\", 1,\"元素上发生鼠标双击时触发。\", 0)");
		list.add("INSERT INTO sys_column_event_dictionary (id, eventName, triggerFunction, isOnlySupportHtml5, remark,weight) VALUES (\"%s\",\"ondrag\", \"function(){}\", 0,\"元素被拖动时运行的脚本。\", 0)");
		list.add("INSERT INTO sys_column_event_dictionary (id, eventName, triggerFunction, isOnlySupportHtml5, remark,weight) VALUES (\"%s\",\"ondragend\", \"function(){}\", 0,\"在拖动操作末端运行的脚本。\", 0)");
		list.add("INSERT INTO sys_column_event_dictionary (id, eventName, triggerFunction, isOnlySupportHtml5, remark,weight) VALUES (\"%s\",\"ondragenter\", \"function(){}\", 0,\"当元素元素已被拖动到有效拖放区域时运行的脚本。\", 0)");
		list.add("INSERT INTO sys_column_event_dictionary (id, eventName, triggerFunction, isOnlySupportHtml5, remark,weight) VALUES (\"%s\",\"ondragleave\", \"function(){}\", 0,\"当元素离开有效拖放目标时运行的脚本。\", 0)");
		list.add("INSERT INTO sys_column_event_dictionary (id, eventName, triggerFunction, isOnlySupportHtml5, remark,weight) VALUES (\"%s\",\"ondragover\", \"function(){}\", 0,\"当元素在有效拖放目标上正在被拖动时运行的脚本。\", 0)");
		list.add("INSERT INTO sys_column_event_dictionary (id, eventName, triggerFunction, isOnlySupportHtml5, remark,weight) VALUES (\"%s\",\"ondragstart\", \"function(){}\", 0,\"在拖动操作开端运行的脚本。\", 0)");
		list.add("INSERT INTO sys_column_event_dictionary (id, eventName, triggerFunction, isOnlySupportHtml5, remark,weight) VALUES (\"%s\",\"ondrop\", \"function(){}\", 0,\"当被拖元素正在被拖放时运行的脚本。\", 0)");
		list.add("INSERT INTO sys_column_event_dictionary (id, eventName, triggerFunction, isOnlySupportHtml5, remark,weight) VALUES (\"%s\",\"onmousedown\", \"function(){}\", 1,\"当元素上按下鼠标按钮时触发。\", 0)");
		list.add("INSERT INTO sys_column_event_dictionary (id, eventName, triggerFunction, isOnlySupportHtml5, remark,weight) VALUES (\"%s\",\"onmousemove\", \"function(){}\", 1,\"当鼠标指针移动到元素上时触发。\", 0)");
		list.add("INSERT INTO sys_column_event_dictionary (id, eventName, triggerFunction, isOnlySupportHtml5, remark,weight) VALUES (\"%s\",\"onmouseout\", \"function(){}\", 1,\"当鼠标指针移出元素时触发。\", 0)");
		list.add("INSERT INTO sys_column_event_dictionary (id, eventName, triggerFunction, isOnlySupportHtml5, remark,weight) VALUES (\"%s\",\"onmouseover\", \"function(){}\", 1,\"当鼠标指针移动到元素上时触发。\", 0)");
		list.add("INSERT INTO sys_column_event_dictionary (id, eventName, triggerFunction, isOnlySupportHtml5, remark,weight) VALUES (\"%s\",\"onmouseup\", \"function(){}\", 1,\"当在元素上释放鼠标按钮时触发。\", 0)");
		list.add("INSERT INTO sys_column_event_dictionary (id, eventName, triggerFunction, isOnlySupportHtml5, remark,weight) VALUES (\"%s\",\"onmousewheel\", \"function(){}\", 0,\"当鼠标滚轮正在被滚动时运行的脚本。\", 0)");
		list.add("INSERT INTO sys_column_event_dictionary (id, eventName, triggerFunction, isOnlySupportHtml5, remark,weight) VALUES (\"%s\",\"onscroll\", \"function(){}\", 0,\"当元素滚动条被滚动时运行的脚本。\", 0)");
		for(String sql : list) {
			String id = SequenceUtil.getLowerCaseUUID();
			String format = String.format(sql , id);
			System.out.println(format + ";");
		}
	}
}
